
static char RCSId[]="$Id: PopUpScrollView.m,v 1.1.1.1 1993/03/18 03:35:17 davis Exp $";


#import <appkit/Matrix.h>
#import <appkit/PopUpList.h>

#import <objc/List.h>

#import "PopUpScrollView.h"
#import "PlotView.h"

#define UNDEFINED	-1.0	/* See [Control selectedTag]	*/
#define DEFAULTTAG	100	/* Default tag upon init is 100	*/


@implementation PopUpScrollView


- initFrame:(const NXRect *)frameRect
{
    [super initFrame:frameRect];
    oldTag = UNDEFINED;
    return self;
}


- free
{
    /*  
     *  A pop-up button's target is a PopUpList that won't be freed 
     *  when the popUpButton is freed.  So we must free it here.
     */
    [[popUpButton target] free];
    return [super free];
}


- changeScale:sender
{
    int theTag = [sender selectedTag];

    if (theTag != oldTag)  {
	oldTag = theTag;
	return [self setCurrentTag: theTag];
    } else
	return nil;
}


/* 1.00 scaleFactor == 100% */

- changeScaleTo:(float)scaleFactor
{
    [[self docView] scale:scaleFactor];
    return self;
}



- tile
{
    NXRect scrollerRect, buttonRect;

    [super tile];

    if ([popUpButton superview] != self) {
	[self addSubview:popUpButton];
    }

    /*
     *  Now make the hScroller smaller and stick the popUpButton next 
     *  to it.
     */
    [hScroller getFrame:&scrollerRect];
    NXDivideRect(&scrollerRect, &buttonRect, 90.0, 2);
    [hScroller setFrame:&scrollerRect];
    [popUpButton setFrame:&buttonRect];
    
    return self;
}



- update
{
    [self setCurrentTag:[self currentTag]];
    return self;
}



- setCurrentTag:(int)aTag
{
    int	i, count;
    id	cell, cells;
    id value = nil;

    cell = [[popUpButton target] findCellWithTag:aTag];

    /* 
     *  Check to make sure the tag is a valid selection (i.e. that it 
     *  is in the PopUpList.  If not, we don't do anything.  (Note 
     *  that we do not add aTag to the list.)
     */
    
    cells = [[[popUpButton target] itemList] cellList];
    count = [cells count];

    for(i = 0; i < count; i++)  {
	if ([cells objectAt:i] == cell)  {
	    [popUpButton setTitle: [cell title]];
	    value = self;
	    [self changeScaleTo: [cell tag] / 100.0];
	    oldTag = aTag;
	    break;
	}
    }

    return value;
}


- (int)currentTag
{
    int aTag;

    aTag = [[[popUpButton target] itemList] selectedTag];

    if (aTag != UNDEFINED)
	return aTag;
    else
	return DEFAULTTAG;
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end
